package com.learncloud.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

public class NettyHttpServerDemo {

    public void start(int port) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {

            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
//                            ch.pipeline().addLast("codec", new HttpServerCodec())  // HTTP 编解码
//                                    .addLast("compressor", new HttpContentCompressor()) // HttpContent 压缩
//                                    .addLast("aggregator", new HttpObjectAggregator(65536)) // HTTP 消息聚合
//                                    .addLast("handler", new HttpServerHandler())
//                            ;
                            ch.pipeline().addLast(new FixedLengthFrameDecoder(5))
                                    .addLast("handler",new EchoServerHandler());

//                            ByteBuf delimiter = Unpooled.copiedBuffer("&".getBytes());
//                           ch.pipeline().addLast(new DelimiterBasedFrameDecoder(10, true, true, delimiter));
//                           ch.pipeline().addLast("handler",new EchoServerHandler());
                        }
                    });
            ChannelFuture f = bootstrap.bind().sync();
            System.out.println("Http Server started， Listening on " + port);
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

    }

    public static void main(String[] args) {
       new NettyHttpServerDemo().start(8888);
//        ByteBuffer buffer = ByteBuffer.allocateDirect(10 * 1024 * 1024);

    }
}
